//
//  RELocalViewController.m
//  录音
//
//  Created by ywy on 2016/12/25.
//  Copyright © 2016年 Wely. All rights reserved.
//

#import "RELocalViewController.h"
#import "RELocalTopTitleView.h" // topTitleView
#import "REDateViewController.h" // 日期VC
#import "RENotClassifiedViewController.h" // 未归类界面
#import "REAlbumViewController.h" // 专辑VC




@interface RELocalViewController ()<UIScrollViewDelegate>
{
    UIScrollView * _topScrollerView;
    UIScrollView * _contentScrollerView;
}
@property(nonatomic,strong) NSArray * titleArr;
@property(nonatomic,assign) NSInteger numberIndex;
@end

@implementation RELocalViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popVC)   name:@"popThree" object:nil];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stopPlayNoti) name:@"stopPlay" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _titleArr = @[@"按日期",@"按专辑",@"未归类"];
    [self setUpScrollView];
    // Do any additional setup after loading the view from its nib.
}
-(void)popVC
{
 
    
    if (self.numberIndex == 0) {
        RELocalTopTitleView * viewFirst = _topScrollerView.subviews[0];
        viewFirst.dateLabel.textColor = RERGBColor(174, 174, 174);;
        viewFirst.dateView.image = [UIImage imageNamed:@"灯泡未选中"];
        //    //6.添加默认子控制器 (也就是第一个)
        //    REDateViewController *vc = self.childViewControllers[2];
        //    vc.view.frame = _contentScrollerView.bounds;
        
        RELocalTopTitleView * view = _topScrollerView.subviews[2];
        view.dateLabel.textColor = [UIColor redColor];
        view.dateView.image = [UIImage imageNamed:@"灯泡选中"];
        
           [_contentScrollerView setContentOffset:CGPointMake(REScreenW*2, 0) animated:YES];
        
    }else if (self.numberIndex == 1)
    {
        RELocalTopTitleView * viewFirst = _topScrollerView.subviews[1];
        viewFirst.dateLabel.textColor = RERGBColor(174, 174, 174);;
        viewFirst.dateView.image = [UIImage imageNamed:@"灯泡未选中"];
        //    //6.添加默认子控制器 (也就是第一个)
        //    REDateViewController *vc = self.childViewControllers[2];
        //    vc.view.frame = _contentScrollerView.bounds;
        
        RELocalTopTitleView * view = _topScrollerView.subviews[2];
        view.dateLabel.textColor = [UIColor redColor];
        view.dateView.image = [UIImage imageNamed:@"灯泡选中"];
         [_contentScrollerView setContentOffset:CGPointMake(REScreenW*2, 0) animated:YES];
        
    }else if (self.numberIndex == 2)
    {
//        RELocalTopTitleView * viewFirst = _topScrollerView.subviews[0];
//        viewFirst.dateLabel.textColor = RERGBColor(174, 174, 174);;
//        viewFirst.dateView.image = [UIImage imageNamed:@"灯泡未选中"];
//        //    //6.添加默认子控制器 (也就是第一个)
//        //    REDateViewController *vc = self.childViewControllers[2];
//        //    vc.view.frame = _contentScrollerView.bounds;
//        
//        RELocalTopTitleView * view = _topScrollerView.subviews[2];
//        view.dateLabel.textColor = [UIColor redColor];
//        view.dateView.image = [UIImage imageNamed:@"灯泡选中"];
//         [_contentScrollerView setContentOffset:CGPointMake(REScreenW*0, 0) animated:YES];
    }
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"popThree" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refrshNot" object:nil];
}

-(void)setUpScrollView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 添加底部的 滑动 视图的
    _topScrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, REScreenW, 40)];
    _topScrollerView.showsHorizontalScrollIndicator=NO;
    _topScrollerView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_topScrollerView];
    //2. 添加大的滚动菜单栏
    _contentScrollerView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, REScreenW, REScreenH-64)];
    _contentScrollerView.showsHorizontalScrollIndicator=NO;
    _contentScrollerView.delegate=self;
    _contentScrollerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_contentScrollerView];
    //3.添加子控制器
    [self addController];
    //4.添加标题栏
    [self addTitleView];
    
    //5.设置大的scrollview的滚动范围
    CGFloat contentX = self.childViewControllers.count * [UIScreen mainScreen].bounds.size.width;
    _contentScrollerView.contentSize = CGSizeMake(contentX, 0);
    _contentScrollerView.pagingEnabled = YES;
    //6.添加默认子控制器 (也就是第一个)
    REDateViewController *vc = [self.childViewControllers firstObject];
    vc.view.frame = _contentScrollerView.bounds;
    RELocalTopTitleView * view = _topScrollerView.subviews[0];
    view.dateLabel.textColor = [UIColor redColor];
    view.dateView.image = [UIImage imageNamed:@"灯泡选中"];
    [_contentScrollerView addSubview:vc.view];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popVC)   name:@"popTwo" object:nil];
    
}
-(void)addController
{
    for (NSInteger i = 0; i<_titleArr.count; i++) {
        if (i==0) {
          REDateViewController   * dateVC = [[REDateViewController alloc] init];
            [self addChildViewController:dateVC];
        }else if(i == 1){
            REAlbumViewController * LocalVC = [[REAlbumViewController alloc] init];
            [self addChildViewController:LocalVC];
        }else
        {
            RENotClassifiedViewController * notClassifiedVC = [[RENotClassifiedViewController alloc] init];
            [self addChildViewController:notClassifiedVC];
        }
    }
}
-(void)addTitleView
{
    CGFloat lblX =0;
    CGFloat titleViewWith = REScreenW/_titleArr.count;
    
    for (NSInteger i = 0 ; i< _titleArr.count; i++) {
        lblX = i * titleViewWith;
        RELocalTopTitleView * titleView = [[RELocalTopTitleView alloc] initWithFrame:CGRectMake(lblX, 0, titleViewWith, 32)];
        titleView.dateLabel.text = _titleArr[i];
        titleView.dateView.image = [UIImage imageNamed:@"灯泡未选中"];
        [_topScrollerView addSubview:titleView];
        titleView.tag = i;
        titleView.userInteractionEnabled = YES;
        [titleView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleViewClick:)]];        
    }
}

-(void)titleViewClick:(UITapGestureRecognizer*)tap
{
    RELocalTopTitleView *titleView = (RELocalTopTitleView *)tap.view;
    for ( RELocalTopTitleView *titleViewTwo in _topScrollerView.subviews) {
        titleViewTwo.dateLabel.textColor =RERGBColor(174, 174, 174) ;
        titleViewTwo.dateView.image = [UIImage imageNamed:@"灯泡未选中"];
    }
        titleView.dateLabel.textColor = [UIColor redColor];
        titleView.dateView.image = [UIImage imageNamed:@"灯泡选中"];
    CGFloat offsetX = titleView.tag * _contentScrollerView.frame.size.width;
    CGFloat offsetY = _contentScrollerView.contentOffset.y;
    CGPoint offset = CGPointMake(offsetX, offsetY);
      self.numberIndex = titleView.tag;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"reFresh" object:nil userInfo:@{@"number":[NSString stringWithFormat:@"%ld",(long)titleView.tag]}];
    [_contentScrollerView setContentOffset:offset animated:YES];
    //滑动的时候 进行界面播放音频的暂停
    [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlay" object:nil];
}

#pragma mark - ******************** scrollView代理方法
/** 滚动结束（手势导致） */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 获得索引
    NSUInteger index = scrollView.contentOffset.x / _contentScrollerView.frame.size.width;
    self.numberIndex = index;
    for ( RELocalTopTitleView *titleView in _topScrollerView.subviews) {
        titleView.dateLabel.textColor = RERGBColor(174, 174, 174);
        titleView.dateView.image = [UIImage imageNamed:@"灯泡未选中"];
    }
    // 滚动标题栏
    RELocalTopTitleView *titleLable = (RELocalTopTitleView *)_topScrollerView.subviews[index];
    titleLable.dateLabel.textColor = [UIColor redColor];
     titleLable.dateView.image = [UIImage imageNamed:@"灯泡选中"];
    // 添加控制器
    REDateViewController *newsVc = self.childViewControllers[index];
    if (newsVc.view.superview) return;
    newsVc.view.frame = scrollView.bounds;
    
    [_contentScrollerView addSubview:newsVc.view];
    //滑动的时候 进行界面播放音频的暂停
    [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlay" object:nil];
    
}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 获得索引
    NSUInteger index = scrollView.contentOffset.x / _contentScrollerView.frame.size.width;
      self.numberIndex = index;
    for ( RELocalTopTitleView *titleView in _topScrollerView.subviews) {
        titleView.dateLabel.textColor = RERGBColor(174, 174, 174);
        titleView.dateView.image = [UIImage imageNamed:@"灯泡未选中"];
    }
    // 滚动标题栏
    RELocalTopTitleView *titleLable = (RELocalTopTitleView *)_topScrollerView.subviews[index];
    titleLable.dateLabel.textColor = [UIColor redColor];
    titleLable.dateView.image = [UIImage imageNamed:@"灯泡选中"];
    // 添加控制器
    REDateViewController *newsVc = self.childViewControllers[index];
    if (newsVc.view.superview) return;
    newsVc.view.frame = scrollView.bounds;
    //滑动的时候 进行界面播放音频的暂停
    [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlay" object:nil];
    [_contentScrollerView addSubview:newsVc.view];
}

//-(void)stopPlayNoti
//{
//    [self performSelector:@selector(stopPlayW) withObject:self afterDelay:0.1];
//}
//-(void)stopPlayW
//{
//     [[NSNotificationCenter defaultCenter] postNotificationName:@"stopPlay" object:nil];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
